package com.bruce.downloader.download;

import com.bruce.downloader.handle.FileResponseExtractor;
import com.bruce.downloader.print.SimpleDownloadProcessInfo;
import com.bruce.downloader.thread.DownloadProcessInfoThread;
import com.bruce.downloader.util.HttpClientUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * 文件下载
 * <br>CreateDate  2021/09/15 17:41:18
 *
 * @author luotuan
 * @version 1.0.0
 */
public class FileDownloader extends AbstractDownloader {
    public FileDownloader(RestTemplate restTemplate) {
        super(restTemplate);
    }
    public FileDownloader(){
        super(HttpClientUtils.getRestTemplate());
    }

    @Override
    protected void doDownload(String decodeUrl, String dirPath, String fileName, HttpHeaders headers) {
        try {
            String filePath = dirPath + File.separator + fileName;
            FileResponseExtractor extractor = new FileResponseExtractor(filePath);
            File tmpFile = restTemplate.execute(decodeUrl, HttpMethod.GET, null, extractor);
            System.out.println();
            tmpFile.renameTo(new File(filePath));
        }finally {
            scheduledExecutorService.shutdown();
        }
    }

}
